package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Lyrics;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.jinxin.phonecloudmusic.myapplication.utils.lrcview.LrcView;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * Created by ZJ on 2017/10/9 0009.
 */

public class PlayMusicLyricsFragment extends BaseFragment{
    @BindView(R.id.lrc_view)
    LrcView lrcView;

    private Lyrics lyrics;
    private long cur;
    private long sum;
    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fra_music_lrc, container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*if (null!=getArguments()){
            lyrics= (Lyrics) getArguments().getSerializable("lyrics");
            lrcView.loadLrc(lyrics.getLrcContent());
        }*/
        //歌词
        //handler.sendEmptyMessage(1);
    }

    //创建handler循环更新
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    handler.sendEmptyMessage(1);//更新歌词进度
                    //handler.post(runnable);
                    break;
                case 1://歌词进度
                    //RxBus.getDefault().post(new MusicControlEvent(5, 1, a));//获取进度
                    //if (slide==false)
                    if (null!=lrcView)
                    lrcView.updateTime(cur);
                    handler.sendEmptyMessage(1);//歌词进度控制
                    break;
                    default:
                        break;
            }
        }
    };


    @Override
    public void initData() {
        //进度控制
        RxBus.getDefault().register(MusicControlEvent.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<MusicControlEvent>() {
            @Override
            public void accept(MusicControlEvent event) throws Exception {
                if ((event.getTag()) == 7) {//7：进度控制
                    sum= (int) event.getObj1();
                    cur= (int) event.getObj2();
                    Log.e("7",cur+"");
                }else if ((event.getTag())==10){
                    if (null!=lrcView) {
                        lrcView.loadLrc((String) event.getObj1());
                        handler.sendEmptyMessage(1);
                    }
                }
            }
        });


    }

    //歌词
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {


            } catch (Exception e) {
                //mediaPlayer = null;
            }

            handler.postDelayed(this, 100);
        }
    };
}
